package com.js.nickcar.module.addcar.brand;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.js.nickcar.R;
import com.js.nickcar.base.BaseFragment;
import com.js.nickcar.entity.remote.CarBrandBean;
import com.js.nickcar.entity.remote.CarModelBean;
import com.js.nickcar.module.addcar.model.CarModelFragment;
import com.js.nickcar.module.addcar.series.CarBrandSeriesFrangment;

import java.util.ArrayList;

import butterknife.Bind;

import static com.js.nickcar.module.addcar.baseinfo.BaseCarInfoFragment.REQUEST_CODE_CAR_MODEL;

/**
 * Created by LZY on 2017/9/29.
 */

public class CarBrandFragment extends BaseFragment<CarBrandPresenter> implements CarBrandView, SwipeRefreshLayout.OnRefreshListener, CarBrandAdapter.OnItemClickListener {

    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.refreshLayout)
    SwipeRefreshLayout mRefreshLayout;

    public CarBrandAdapter mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_car_brand;
    }

    @Override
    protected CarBrandPresenter createPresenter() {
        return new CarBrandPresenter();
    }

    @Override
    protected void initData() {
        mRefreshLayout.setColorSchemeResources(
                R.color.swiperefresh_color1, R.color.swiperefresh_color2,
                R.color.swiperefresh_color3, R.color.swiperefresh_color4);
        mAdapter = new CarBrandAdapter(getContext());
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setRefreshing(true);
        mRecyclerView.setAdapter(mAdapter);
        onRefresh();
    }

    @Override
    public void updateHot(ArrayList<CarBrandBean> hotLists) {
        mAdapter.updateHot(hotLists);
    }

    @Override
    public void updateCarBrand(ArrayList<CarBrandBean> carBrandLists) {
        mAdapter.addAll(carBrandLists);
    }

    @Override
    public void onComplete() {
        mRefreshLayout.setRefreshing(false);
    }

    @Override
    protected String getTitle() {
        return getString(R.string.select_car_type);
    }

    public static Fragment getInstance() {
        return new CarBrandFragment();
    }

    @Override
    public void onRefresh() {
        mPresenter.requestData();
    }

    @Override
    public void onItemClick(CarBrandBean item, int position,int itemType) {
        CarBrandSeriesFrangment seriesFrangment = CarBrandSeriesFrangment.getInstance(item);
        seriesFrangment.setTargetFragment(this,getTargetRequestCode());
        addFragment(seriesFrangment);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && resultCode == Activity.RESULT_OK && requestCode == getTargetRequestCode()) {
            getTargetFragment().onActivityResult(requestCode,resultCode,data);
            finish();
        }
    }
}
